#include "recformitem.h"
#include "qdebug.h"
#include "ui_recformitem.h"

recFormitem::recFormitem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::recFormitem)
{
    ui->setupUi(this);
    ui->widget_2->installEventFilter(this);
}

recFormitem::~recFormitem()
{
    delete ui;
}

bool recFormitem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->widget_2)
    {
        if(event->type() == QEvent::Enter)
        {
            // qDebug() << "jinlai";
            //添加上移动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->widget,"geometry",this);
            animation->setDuration(300);
            animation->setStartValue(QRect(10,10,ui->widget->width(),ui->widget->height()));
            animation->setEndValue(QRect(10,0,ui->widget->width(),ui->widget->height()));
            animation->start();

            //销毁对象树
            connect(animation,&QPropertyAnimation::finished,this,[=](){ delete animation;});
        }
        else if(event->type() == QEvent::Leave)
        {
            // qDebug() << "likai";
            //添加下移动画
            QPropertyAnimation* animation = new QPropertyAnimation(ui->widget,"geometry",this);
            animation->setDuration(300);
            animation->setStartValue(QRect(10,0,ui->widget->width(),ui->widget->height()));
            animation->setEndValue(QRect(10,10,ui->widget->width(),ui->widget->height()));
            animation->start();

            //销毁对象树
            connect(animation,&QPropertyAnimation::finished,this,[=](){ delete animation;});
        }
        return false;
    }
    return QWidget::eventFilter(watched, event);
}

void recFormitem::setItemImg(const QString &imagePath)
{
    QString style = "border-image:url(:/pic/"+imagePath+");";
    // qDebug() << "Style: " << style;
    ui->widget_2->setStyleSheet(style);
}

void recFormitem::setItemText(const QString &text)
{
    ui->label->setText(text);
}
